#include "terrain.h"
#include "bombe.h"
#include "ennemi.h"
#include "chemin.h"
#include <malloc.h>
#include <assert.h>

void terrainInit(terrain* ter, const int numeroTerrain, const int mode)
{
    int x, y, i,j;

    /* ---------------- Choix du terrain ---------------- */
        FILE* f;
        char terrain[15][15];
        f = 0;

        switch (numeroTerrain)
        {
            case 1: f = fopen("data/1.txt", "r"); break;
            case 2: f = fopen("data/2.txt", "r"); break;
            case 3: f = fopen("data/3.txt", "r"); break;
            case 4: f = fopen("data/4.txt", "r"); break;
            case 5: f = fopen("data/5.txt", "r"); break;
        }
        assert(f);

        for (i = 0; i<15; i++)
        {
            for (j = 0; j<17; j++)
            {
                terrain[i][j] = fgetc(f);
            }
        }
        fclose(f);
    /* ---------------------------------------------------*/

	ter->tailleX = 15;
	ter->tailleY = 15;

    /* remplissage du tableau */
        ter->terrainTab = (char **)malloc(sizeof(char *)*ter->tailleY);
        for (y=0; y<ter->tailleY; y++)
            ter->terrainTab[y] = (char *)malloc(sizeof(char)*ter->tailleX);
        assert(ter->terrainTab);

	/* initialisation de numCheminTab */
        ter->numCheminTab = (chemin**)malloc(sizeof(int*)*ter->tailleY);
        assert(ter->numCheminTab);
		for (y=0; y<ter->tailleY; y++)
            ter->numCheminTab[y] = (chemin *)malloc(sizeof(chemin)*ter->tailleX);
        initNumCheminTab(ter->numCheminTab,ter->tailleX ,ter->tailleY);

    /* initialisation de dangerTab */
    	ter->dangerTab = (int**)malloc(sizeof(int*)*ter->tailleY);
        assert(ter->dangerTab);
		for (y=0; y<ter->tailleY; y++)
            ter->dangerTab[y] = (int *)malloc(sizeof(int)*ter->tailleX);
        initDangerTab(ter->dangerTab,ter->tailleX ,ter->tailleY);

    /* initialisation du tableau d'ennemis */
    for (i = 0; i < 5; i++)
        ter->ennemiTab[i] = 0;

	/* initialisation du tableau de bombes */
	for (i = 0;i < 7; i++)
        ter->bombeTab[i] = 0;

    /* initialisation du tableau de bonus */
    for (i = 0; i< 4; i++)
        ter->bombeTab[i] = 0;

	for(y = 0 ; y < ter->tailleY ; y++)
    {
		for(x = 0 ; x < ter->tailleX ; x++)
        {
			ter->terrainTab[y][x] = terrain[y][x];
        }
    }

    if (mode == 1 || mode == 3)
    {
        /* on met des ennemis si on n'est pas en mode 2 joueurs ou si on est en mode coop */
        ennemi* ennemi1 = 0;
        ennemi* ennemi2 = 0;
        switch(numeroTerrain)
        {
            case 1 : ennemi1 = ennemiInit(ter, 1, 13); break;
            case 2 : ennemi1 = ennemiInit(ter, 1, 13); break;
            case 3 : ennemi1 = ennemiInit(ter, 1, 13); break;
            case 4 : ennemi1 = ennemiInit(ter, 1, 13);
                     ennemi2 = ennemiInit(ter, 13, 1); break;
            case 5 : ennemi1 = ennemiInit(ter, 13, 13);
                     ennemi2 = ennemiInit(ter, 13, 1); break;
        }
    }
}

void terrainLibere(terrain* ter)
{
	int y;
	for (y=0; y<ter->tailleY; y++)
		free(ter->terrainTab[y]) ;
	free(ter->terrainTab) ;
	ter->tailleX = 0 ;
	ter->tailleY = 0 ;
	ter->terrainTab = NULL ;
}

int terrainPositionValide(const terrain* ter, const int x, const int y)
{
	if (x >= 0 && x<ter->tailleX && y >= 0 && y<ter->tailleY
     && ter->terrainTab[y][x] != '#'
     && ter->terrainTab[y][x] != 'C'
     && ter->terrainTab[y][x] != 'o'
     && ter->terrainTab[y][x] != '0'
     && ter->terrainTab[y][x] != '1'
     && ter->terrainTab[y][x] != '2'
     && ter->terrainTab[y][x] != '3')
		return 1 ;
	else
		return 0 ;
}

char terrainGetXY(const terrain* ter, const int x, const int y)
{
	assert(x >= 0) ;
	assert(y >= 0) ;
	assert(x<ter->tailleX) ;
	assert(y<ter->tailleY) ;
	return ter->terrainTab[y][x] ;
}

void terrainSetXY(terrain* ter, const int x, const int y, const char objet)
{
	assert(x >= 0) ;
	assert(y >= 0) ;
	assert(x <ter->tailleX);
	assert(y <ter->tailleY);
	ter->terrainTab[y][x] = objet ;
}

const int getTailleX(const terrain *pTer)
{
	return pTer->tailleX;
}

const int getTailleY(const terrain *pTer)
{
	return pTer->tailleY;
}

int compteCaisses(const terrain* ter)
{
    int nbCaisses = 0;
    int i = 0;
    int j = 0;
	for (i = 0; i < getTailleY(ter); i++)
    {
        for (j = 0; j < getTailleX(ter); j++)
        {
            if (terrainGetXY(ter,i,j) == 'C')
            {
                nbCaisses++;
            }
        }
    }
    return nbCaisses;
}

int estZoneDanger(int **tab, const int x, const int y) /* return 1 si zone danger */
{
    if(tab[y][x] == 1)
        return 1;
    else return 0;
}

void initDangerTab(int **dangerTab, const int x, const int y)
{
    int i,j;
    for(i=0;i<x;i++)
    {
        for(j=0;j<y;j++)
        {
            dangerTab[i][j] = 0;
        }
    }
}

void remplirDangerTab(terrain* ter, const int puissance)
{
    int i,x,y;
    initDangerTab(ter->dangerTab,15,15);
    for(i=0;i<7;i++)
    {
        if(ter->bombeTab[i] != 0)
        {
            x = ter->bombeTab[i]->x;
            y = ter->bombeTab[i]->y;
            ter->dangerTab[y][x] = 1;
            if(x>0) ter->dangerTab[y][x-1] = 1;
            if(x<15) ter->dangerTab[y][x+1] = 1;
            if(y>0) ter->dangerTab[y-1][x] = 1;
            if(y<15) ter->dangerTab[y+1][x] = 1;
            if(puissance>0)
            {

                if(x>1) ter->dangerTab[y][x-2] = 1;
                if(x<14) ter->dangerTab[y][x+2] = 1;
                if(y>1) ter->dangerTab[y-2][x] = 1;
                if(y<13) ter->dangerTab[y+2][x] = 1;
            }

        }
    }
}
